import {
	request
} from '../../network/index'


Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		show: false,
		MvUrl: '',
		info: '',
		data: '',
		id: '',
		is: false,
		comment: [],
		value: '',
		offset: 0,
		placeholder:'用评论鼓励一下TA吧~',
		commentId:''
	},
	//发射评论
	async send() {
		if(this.data.value.trim()){
			let send = await request('/comment', {
				type: 1,
				t:1,
				id: this.data.id,
				content: this.data.value,
				commentId:this.data.commentId
			})
			console.log(send);
			this.data.comment.unshift(send.data.comment)
			this.setData({
				comment: this.data.comment,
				value:''
			})
		}
	
	},
	//删除评论
	long(event) {
		let index = event.currentTarget.dataset.index
		wx.showModal({
			title: '提示',
			content: '是否删除',
			success: async (res) => {
				try {
					if (res.confirm) {
						let commentId = event.currentTarget.dataset.commentid
						let remove = await request('/comment', {
							type: 1,
							t: 0,
							id: this.data.id,
							commentId
						})
						if(remove.data.code == 200){
							this.data.comment.splice(index,1)
							this.setData({
								comment:this.data.comment
							})
						}else{
							wx.showToast({
								title: remove.data.message,
								icon:'error'
							})
						}
					}
				} catch(error) {
					console.log(error);
					console.log('错误');
				}
			}
		})
	},
	//回复评论
	getComment(event){
		let placeholder = event.currentTarget.dataset.user
		let id = event.currentTarget.dataset.commentid
		this.data.commentId = id
		console.log(this.data.commentId );
		this.setData({
			placeholder:`回复${placeholder}:`
		})
	},
	onClose() {
		this.setData({
			show: false,
		})
	},
	async showPopup() {
		let comment = await request('/comment/mv', {
			id: this.data.id,
			limit: 10
		})
		console.log(comment);
		this.setData({
			show: true,
			comment: comment.data.comments
		});
	},
	//加载更多评论
	async scrolltoupper() {
		this.data.offset += 10
		let comment = await request('/comment/mv', {
			id: this.data.id,
			limit: 10,
			offset: this.data.offset
		})
		this.data.comment.push(...comment.data.comments)
		this.setData({
			comment: this.data.comment
		})
	},
	//点赞
	async like() {
		this.setData({
			is: !this.data.is,
		})
	},
	onLoad: async function (options) {
		this.data.id = options.id
		let mvData = await request('/mv/detail', {
			mvid: options.id
		})
		let mvUrl = await request('/mv/url', {
			id: options.id
		})
		let mvInfo = await request('/mv/detail/info', {
			mvid: options.id
		})
		this.setData({
			mvUrl: mvUrl.data.data.url,
			info: mvInfo.data,
			data: mvData.data.data
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})